﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 用户注册
    /// </summary>
    public class RegisterController : ApiController
    {
        private ApiContext db = new ApiContext();  
        /// <summary>
        /// 食客注册
        /// </summary>
        /// <param name="uUser">食客实体</param>
        /// <returns></returns>
        public JsonData Post(U_User uUser)
        {
            var jsonData = new JsonData();
            try
            {
                if (ModelState.IsValid)
                {
                    uUser.Reg_Time = DateTime.Now;
                    uUser.Reg_IP = "";
                    uUser.Exp = 100;
                    uUser.Birthday = DateTime.Now;
                    uUser.Sex = "男";
                    uUser.Status = 1;
                    uUser.U_Name = uUser.U_Uid;
                    uUser.Nick_Name = uUser.U_Uid;
 

                    var item= db.U_User.Add(uUser);
                    db.SaveChanges();
                    jsonData.Success = true;
                    jsonData.Data = item;
                }
            }
            catch
            {
                jsonData.Success = false;
                jsonData.Error = "注册失败";
            }
            return jsonData;
        } 
        /// <summary>
        /// 判断是否已经注册
        /// </summary>
        /// <param name="type">email，mobile，uid</param>
        /// <param name="value">需要验证的数据</param>
        /// <returns></returns>
        public JsonData GetIsReg(string type, string value)
        {
            var jsonData=new JsonData{Success = true};
            U_User item = null;
            if (type.ToLower() == "email")
            {
                  item = db.U_User.FirstOrDefault(c => c.Email == value);
            }
            if (type.ToLower() == "mobile")
             {
                 item = db.U_User.FirstOrDefault(c => c.Mobile == value);
             }
            if (type.ToLower() == "uid")
             {
                 item = db.U_User.FirstOrDefault(c => c.U_Uid == value);
             } 
            if (item != null)
            {
                jsonData.Success = false; 
                jsonData.Error = type + "[" + value + "]" + "已存在!";
            }
            return jsonData;
        }
    }
}
